from tkinter import Tk
from tkinter import ttk
from database.table import tablee
from database.table.tablee import Commodity

def CountCommodity():
    count_commodity_screen = Tk()
    count_commodity_screen.title('统计商品信息')
    screen_width, screen_height = count_commodity_screen.maxsize()
    # 窗口的宽和高
    width = 605
    height = 200
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    count_commodity_screen.geometry(centre)
    count_commodity_screen.resizable(0,0)

    commodityy = tablee.session.query(Commodity).all()
    data = Commodity.to_json(commodityy)
    length = len(data)

    tree = ttk.Treeview()  # 创建表格对象
    tree["columns"] = ("编号", "名称", "单价", "供应商号", "简介", "备注")
    tree.column("编号", width=50)  # 设置列
    tree.column("名称", width=50)
    tree.column("单价", width=50)
    tree.column("供应商号", width=100)
    tree.column("简介", width=50)
    tree.column("备注", width=50)
    tree.heading("编号", text="编号")  # 显示表头名
    tree.heading("名称", text="名称")
    tree.heading("单价", text="单价")
    tree.heading("供应商号", text="供应商号")
    tree.heading("简介", text="简介")
    tree.heading("备注", text="备注")

    j = length - 1

    for i in range(0, length):
        num = data[j]['Cno']
        name = data[j]['Cname']
        unitprice = data[j]['Cunitprice']
        sno = data[j]['Sno']
        describe = data[j]['Cdescribe']
        remarks = data[j]['Cremarks']
        tree.insert("", 0, text='', value=(num, name, unitprice, sno, describe, remarks))
        j = j -1
    tree.pack()

    tablee.session.close()

    count_commodity_screen.mainloop()

if __name__ == '__main__':
    CountCommodity()